package fhb.mobile.todoapp;

import java.util.Date;

import fhb.mobile.todoapp.datamodell.ToDoItem;
import fhb.mobile.todoapp.datamodell.ToDoItemList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TimePicker;

public class ChooseDeadlineActivity extends Activity {
	
	private TimePicker deadline_time = null;
	private DatePicker deadline_date = null;
	private Button saveButton = null;
	private ToDoItem modifiedToDoItem = null;
	
	@Override
	public void onBackPressed() {
		startActivity(new Intent(ChooseDeadlineActivity.this, EditToDoItemActivity.class));
		
		super.onBackPressed();
	}
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.todoliste_choose_deadline);
        
        deadline_time = (TimePicker) findViewById(R.id.todolist_choose_deadlinetimePicker);
        deadline_date = (DatePicker) findViewById(R.id.todolist_choose_deadlinedatePicker);
        saveButton = (Button) findViewById(R.id.todolist_choose_deadline_save);
        
        modifiedToDoItem = ToDoItemList.getModifiedToDoItem();
        Date deadline = new Date(modifiedToDoItem.getDeadline());
        
        deadline_time.setCurrentHour(deadline.getHours());
        deadline_time.setCurrentMinute(deadline.getMinutes());
        deadline_date.updateDate(deadline.getYear() + 1900, deadline.getMonth(), deadline.getDate());
        
        saveButton.setOnClickListener(
        	new OnClickListener() {
					
				@Override
				public void onClick(View v) {
					Date deadline = new Date();
					deadline.setHours(deadline_time.getCurrentHour());
					deadline.setMinutes(deadline_time.getCurrentMinute());
					deadline.setDate(deadline_date.getDayOfMonth());
					deadline.setMonth(deadline_date.getMonth());
					deadline.setYear(deadline_date.getYear()-1900);
					
					modifiedToDoItem.setDeadline(deadline.getTime());
					
					Intent intent = new Intent(ChooseDeadlineActivity.this, EditToDoItemActivity.class);					
					startActivity(intent);
					finish();
				}
        	}
        );
    }
}
